package com.hksj.wmp.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.base.query.BaseSpecialityQuery;
import com.hksj.cockpit.service.ComprehensiveCockpitService;
import com.hksj.wmp.mapper.WeChatAlarmMapper;
import com.hksj.wmp.query.BaseAlarmQuery;
import com.hksj.wmp.vo.CurrentAlarmDeviceVO;
import com.hksj.wmp.vo.CurrentParseInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zengke
 * @version 1.0
 * @see WeChatAlarmService
 * @since 2024/10/15 10:13
 */

@Service
public class WeChatAlarmService {

    @Autowired
    private ComprehensiveCockpitService comprehensiveCockpitService;

    @Autowired
    private WeChatAlarmMapper wechatAlarmMapper;

    /**
     * 查询报警设备分布
     * @param query 基础专业查询类
     * @return Map集合 key:各专业code,value:专业对应的报警数量
     */
    public List<Map<String, String>> queryAlarmDeviceDistribution(BaseAlarmQuery query) {
        return wechatAlarmMapper.queryAlarmDeviceDistribution(query);
    }

    /**
     * 查询报警次数情况
     */
    public Map<String, String> queryAlarmFrequencySituation(BaseSpecialityQuery query){

        Map<String, String> countMap = new HashMap<>(3);


        //查询今日报警数量
        String todayAlarmCount = wechatAlarmMapper.queryAlarmFrequencySituation(query, 0);

        //查询本周报警数量
        String weekAlarmCount = wechatAlarmMapper.queryAlarmFrequencySituation(query, 1);

        //查询本月报警数量
        String monthAlarmCount = wechatAlarmMapper.queryAlarmFrequencySituation(query, 2);

        //封装报警次数情况
        countMap.put("todayAlarmCount",todayAlarmCount);
        countMap.put("weekAlarmCount",weekAlarmCount);
        countMap.put("monthAlarmCount",monthAlarmCount);

        //返回报警次数情况
        return countMap;
    }

    /**
     * 分页查询当前报警设备列表
     * @param query 查询条件
     * @return 当前报警设备列表
     */
    public IPage<CurrentAlarmDeviceVO> queryCurrentAlarmDeviceListByPaging(BaseSpecialityQuery query) {

        //创建分页对象
        IPage<CurrentAlarmDeviceVO> page = new Page<>(query.getPageNum(),query.getPageSize());

        //分页查询当前报警设备信息
        List<CurrentAlarmDeviceVO> currentAlarmDeviceList = wechatAlarmMapper.queryCurrentAlarmDeviceListByPaging(page,query);

        //判断设备信息是否为空
        if (ObjectUtils.isEmpty(currentAlarmDeviceList)){
            //若设备信息为空
            return page;
        }

        //设备信息不为空,获取设备id组成的集合
        List<String> deviceIdList = currentAlarmDeviceList.stream().map(CurrentAlarmDeviceVO::getDeviceId).collect(Collectors.toList());

        //查询该设备id集合 对应的物模型属性信息
        List<CurrentParseInfoVO> currentParseInfoList = wechatAlarmMapper.queryCurrentParseInfoList(deviceIdList);

        //遍历设备列表和物模型属性列表
        currentAlarmDeviceList.forEach(device -> {
            currentParseInfoList.forEach(currentParseInfo -> {
                //比较物模型属性的deviceId与当前设备列表的id是否一致
                if (Objects.equals(device.getDeviceId(),currentParseInfo.getDeviceId())){

                    //一致,将物模型属性添加到设备列表的物模型中
                    device.getCurrentParseInfoList().add(currentParseInfo);
                }
            });
        });

        //将设备信息设置到page中
        page.setRecords(currentAlarmDeviceList);

        //返回分页结果
        return page;
    }

    /**
     * 查询报警次数详情
     * @param query 查询对象
     * @return 报警的次数列表
     */
    public List<Map<String, String>> queryAlarmFrequencyDetail(BaseAlarmQuery query) {
        return wechatAlarmMapper.queryAlarmFrequencyDetail(query);
    }
}
